package cn.yuanqiao.web.controller.manage;

import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.manage.domain.YqdaMdataGroup;
import cn.yuanqiao.manage.service.IYqdaMdataGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 元数据方案Controller
 * 
 * @author yuanqiao
 * @date 2023-06-06
 */
@RestController
@RequestMapping("/manage/mdataGroup")
public class YqdaMdataGroupController extends BaseController
{
    @Autowired
    private IYqdaMdataGroupService yqdaMdataGroupService;

    /**
     * 查询元数据方案列表
     */
    //@PreAuthorize("@ss.hasPermi('manage:YqdaMdataGroup:list")
    @Log(title = "元数据方案", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public AjaxResult list(YqdaMdataGroup yqdaMdataGroup)
    {
        List<YqdaMdataGroup> list = yqdaMdataGroupService.selectYqdaMdataGroupList(yqdaMdataGroup);
        return AjaxResult.success(list);
    }

    /**
     * 复制到下元数据方案列表
     * @param groupId
     * @param type
     * @return
     */
    @Log(title = "复制到下元数据方案列表", businessType = BusinessType.SELECT)
    @GetMapping("/copyGroupList")
    public AjaxResult copyGroupList(@RequestParam Long groupId, @RequestParam Integer type){
        if(StringUtils.isNull(groupId) || StringUtils.isNull(type)){
            return AjaxResult.error("参数错误！");
        }else {
            List<YqdaMdataGroup> list = yqdaMdataGroupService.copyGroupList(groupId,type);
            return AjaxResult.success(list);
        }
    }

    /**
     * 元数据方案树
     * @param yqdaMdataGroup
     * @return
     */
    @Log(title = "元数据方案树", businessType = BusinessType.SELECT)
    @GetMapping("/groupTree")
    public AjaxResult selectGroupTreeList(YqdaMdataGroup yqdaMdataGroup){
        return success(yqdaMdataGroupService.selectGroupTreeList(yqdaMdataGroup));
    }
    /**
     * 导出元数据方案列表
     */
    @PreAuthorize("@ss.hasPermi('manage:YqdaMdataGroup:export')")
    @Log(title = "元数据方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqdaMdataGroup yqdaMdataGroup)
    {
        List<YqdaMdataGroup> list = yqdaMdataGroupService.selectYqdaMdataGroupList(yqdaMdataGroup);
        ExcelUtil<YqdaMdataGroup> util = new ExcelUtil<YqdaMdataGroup>(YqdaMdataGroup.class);
        util.exportExcel(response, list, "元数据方案数据");
    }

    /**
     * 获取元数据方案详细信息
     */
    //@PreAuthorize("@ss.hasPermi('manage:YqdaMdataGroup:query")
    @Log(title = "获取元数据方案详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqdaMdataGroupService.selectYqdaMdataGroupByID(ID));
    }

    /**
     * 新增元数据方案
     */
    //@PreAuthorize("@ss.hasPermi('manage:YqdaMdataGroup:add")
    @Log(title = "元数据方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody YqdaMdataGroup yqdaMdataGroup)
    {
        return toAjax(yqdaMdataGroupService.insertYqdaMdataGroup(yqdaMdataGroup));
    }

    /**
     * 修改元数据方案
     */
    //@PreAuthorize("@ss.hasPermi('manage:YqdaMdataGroup:edit")
    @Log(title = "元数据方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqdaMdataGroup yqdaMdataGroup)
    {
        if(StringUtils.isNotNull(yqdaMdataGroup.getId())){
            return toAjax(yqdaMdataGroupService.updateYqdaMdataGroup(yqdaMdataGroup));
        }else {
            return  AjaxResult.error("ID不能为空!");
        }

    }

    /**
     * 删除元数据方案
     */
    //@PreAuthorize("@ss.hasPermi('manage:YqdaMdataGroup:remove")
    @Log(title = "元数据方案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ID}")
    public AjaxResult remove(@PathVariable Long ID)
    {
        return toAjax(yqdaMdataGroupService.deleteYqdaMdataGroupByID(ID));
    }
}
